<?php
function bend_view(&$retparams){
	global $_db;
	if(!isset($_POST['op'])){
		return BEND_REDIRECT;
	}
	elseif("addview"==$_POST['op']){
		echo rendertpl("applet/viewaddmod.applet", array("title"=>"添加视图"));
		return BEND_RENDERED;
	}
	elseif("add"==$_POST['op']){
		$id = $_db->insert("view", array("name"=>$_POST['name'], "path"=>$_POST['path'], "desc"=>$_POST['desc'], "title"=>$_POST['title']));
		if($id>0){
			$retparams['errorcode'] = 0;
		}
		else{
			$retparams['errorcode'] = -1;
		}
		return BEND_ECHORET;
	}
	elseif("del"==$_POST['op']){
		$count = $_db->delete("view", "id=$_POST[id]");
	}
	elseif("modview"==$_POST['op']){
		echo rendertpl("applet/viewaddmod.applet", array("title"=>"修改视图"));
		return BEND_RENDERED;
	}
	elseif("mod"==$_POST['op']){
		$count = $_db->update("view"
			,array("name"=>$_POST['name'], "path"=>$_POST['path'], "desc"=>$_POST['desc'], 
				"title"=>$_POST['title'])
			,"id=$_POST[id]");
		if($count>0){
			$retparams['errorcode'] = 0;
		}
		else{
			$retparams['errorcode'] = -1;
		}
		return BEND_ECHORET;
	}
	elseif("search"==$_POST['op']){
		return BEND_HOMEPAGE;
	}
	elseif("autoupdateview"==$_POST['op']){
		//$_db->delete("view", null, true);
		$params = array();
		$viewdir = opendir(ROOT_DIR."/tpl/view");
		while(false!==($view=readdir($viewdir))){
			if("."==$view || ".."==$view) continue;
			$namesuf = explode('.',$view);
			$name = $namesuf[0];
			
			$params['name'] = $name;
			$res = getview($params);
			if(isempty($res))
				$_db->insert("view", array("name"=>$name, "path"=>"view/$view"));
			//TODO 失效不存在的view
		}
	}
	return BEND_REDIRECT;
}
?>